package colab.gui;



import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import colab.dominio.Usuario;
import colab.persistencia.UsuarioDAO;


import com.jgoodies.forms.factories.DefaultComponentFactory;

import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import java.awt.Toolkit;
import javax.swing.JComboBox;

public class TelaCadastroUsuario extends JFrame {

	private JPanel contentPane;
	private JTextField textCpf;
	private JTextField textNome;
	private JTextField textSenha;
	private JTextField textConfirmaSenha;
	private JComboBox comboBox;
	/**
	 * Launch the application.
	 */

	/**
	 * Create the frame.
	 */
	public TelaCadastroUsuario() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(TelaCadastroUsuario.class.getResource("/colab/gui/imagens/COLAB-001.PNG")));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 564, 453);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textCpf = new JTextField();
		textCpf.setToolTipText("");
		textCpf.setBounds(194, 113, 171, 20);
		contentPane.add(textCpf);
		textCpf.setColumns(10);
		
		textNome = new JTextField();
		textNome.setBounds(194, 145, 171, 20);
		contentPane.add(textNome);
		textNome.setColumns(10);
		
		
		JButton btnEntrar = new JButton("Cadastrar");
		btnEntrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int cpf = Integer.parseInt(textCpf.getText());

				String senha = textSenha.getText();

				
			    if(textSenha.getText().equals(textConfirmaSenha.getText())){
					Usuario usuario = new Usuario();
					usuario.setTudo(cpf, 2, senha, textNome.getText());
					try {
						if(UsuarioDAO.cadastrarUsuario(usuario)){
							JOptionPane.showMessageDialog(null, "Cadastrado");
							new TelaLogin().setVisible(true);
							dispose();
						}
					} catch (SQLException e1) {
						
						e1.printStackTrace();
					}
					dispose();
				} else{
					textConfirmaSenha.setText("senhas diferentes");
				}
			}
		});
		btnEntrar.setBounds(215, 268, 110, 23);
		contentPane.add(btnEntrar);
		
		JLabel lblLogin = DefaultComponentFactory.getInstance().createLabel("Login/CPF");
		lblLogin.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLogin.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblLogin.setBounds(92, 113, 92, 17);
		contentPane.add(lblLogin);
		
		JLabel lblSenha = DefaultComponentFactory.getInstance().createLabel("Senha");
		lblSenha.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSenha.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblSenha.setBounds(92, 176, 92, 14);
		contentPane.add(lblSenha);
		
		JLabel lblNovoUsurio = new JLabel("Novo Usu\u00E1rio");
		lblNovoUsurio.setHorizontalAlignment(SwingConstants.CENTER);
		lblNovoUsurio.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblNovoUsurio.setBounds(10, 64, 528, 20);
		contentPane.add(lblNovoUsurio);
		
		textSenha = new JTextField();
		textSenha.setColumns(10);
		textSenha.setBounds(194, 175, 171, 20);
		contentPane.add(textSenha);
		
		JLabel lblEmail = new JLabel("Nome");
		lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblEmail.setHorizontalAlignment(SwingConstants.TRAILING);
		lblEmail.setBounds(138, 145, 46, 14);
		contentPane.add(lblEmail);
		
		textConfirmaSenha = new JTextField();
		textConfirmaSenha.setColumns(10);
		textConfirmaSenha.setBounds(194, 206, 171, 20);
		contentPane.add(textConfirmaSenha);
		
		JLabel lblConfirmaSenha = new JLabel("Confirma senha");
		lblConfirmaSenha.setHorizontalAlignment(SwingConstants.RIGHT);
		lblConfirmaSenha.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblConfirmaSenha.setBounds(82, 207, 102, 14);
		contentPane.add(lblConfirmaSenha);
	
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon("C:\\workspace\\CO-LAB\\src\\colab\\gui\\imagens\\COLAB-001-peq.PNG"));
		label.setBounds(10, 11, 79, 45);
		contentPane.add(label);
		
		JButton btnVoltar = new JButton("Voltar");
		btnVoltar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new TelaLogin().setVisible(true);
				dispose();
			}
		});
		btnVoltar.setBounds(10, 381, 89, 23);
		contentPane.add(btnVoltar);
	}
}
